<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改评分指标项')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-item-edit" th:object="${gradeItem}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">模板名称：</label>
                <div class="col-sm-8">
                    <select id="gradeTemplate" name="templateId" class="form-control m-b" required disabled>
                        <option th:each="pets:${templateList}" th:value="${pets.id}"
                                th:text="${pets.templateName}" th:field="*{templateId}" ></option>
                    </select>

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">分类：</label>
                <div class="col-sm-8">
                    <select id="gradeType" name="typeId" class="form-control m-b" required>
                        <option value="">所有</option>
                        <option th:each="pets:${list}" th:value="${pets.typeId}"
                                th:text="${pets.typeName}" th:field="*{typeId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">评价标准内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{itemContent}" required>
                    <div class="summernote" id="itemContent"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">封顶分数：</label>
                <div class="col-sm-8">
                    <input name="maxScore" th:field="*{maxScore}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">排序号：</label>
                <div class="col-sm-8">
                    <input name="itemOrder" th:field="*{itemOrder}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否可抵扣：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="deductFlag" name="deductFlag"  th:attr ="checked=${gradeItem.deductFlag=='1'?false:true}">
                        <span></span>
                    </label>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/item";
        $("#form-item-edit").validate({
            focusCleanup: true
        });

        var selectedTemplateId = [[${selectedTemplateId}]];
        var selectedTypeId = [[${selectedTypeId}]];

        $("#gradeTemplate").change(function () {//选择模板 填充分类
			$.ajax({
				url: ctx + "system/type/getTypeByTemplateId",
				type: 'GET',
				data: {
					templateId: $("select[name='templateId']").val()
				},
				success:function (result) {
				    console.log(result);
					$("#gradeType").empty();
					$("#gradeType").append("<option value=''>请选分类</option>");
					for (var i = 0; i < result.data.length; i++){
						$("#gradeType").append("<option value=" + result.data[i].typeId + ">" + result.data[i].typeName + "</option>");
					}
				}
			});
		});

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-item-edit').serialize());
            }
        }

        $(function() {
            //$("select[name='templateId']").val(selectedTemplateId);

            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });
    </script>
</body>
</html>